﻿using System.Windows;
using System.Windows.Controls;
using Chenglin.WPChildrenDrug.Models;
using Microsoft.Phone.Controls;

namespace Chenglin.WPChildrenDrug
{
    public partial class DrugInfoPage : PhoneApplicationPage
    {
        public DrugInfoPage()
        {
            InitializeComponent();
            this.Loaded += Page_Loaded;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateScrollerHeight();
        }

        protected override void OnOrientationChanged(OrientationChangedEventArgs e)
        {
            base.OnOrientationChanged(e);
            UpdateScrollerHeight();
        }

        /// <summary>
        /// Update the height of ScrollViewer control. Otherwise, the scroll function won't work as expected. :(
        /// </summary>
        private void UpdateScrollerHeight()
        {
            if (single_RootContainer.Visibility == System.Windows.Visibility.Visible)
            {
                Point point = single_ContentContainer.TransformToVisual(LayoutRoot).Transform(new Point(0, 0));
                single_ContentContainer.Height = this.LayoutRoot.ActualHeight - point.Y;
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Hide all the content first
            single_RootContainer.Visibility = System.Windows.Visibility.Collapsed;
            containerRoot.Visibility = System.Windows.Visibility.Collapsed;

            // Remove all exists content
            containerRoot.Items.Clear();

            string name;
            Drug drug = null;
            NavigationContext.QueryString.TryGetValue("n", out name);

            if (!string.IsNullOrEmpty(name))
            {
                drug = App.Drugs.GetDrug(name);
            }

            if (drug == null || drug.Entries == null || drug.Entries.Length == 0)
            {
                MessageBox.Show("The specified drug does not exist!", "Error", MessageBoxButton.OK);
                NavigationService.GoBack();
            }
            else
            {
                if (drug.Entries.Length == 1)
                {
                    var entry = drug.Entries[0];

                    // The header of the drug entry
                    //
                    StackPanel headerPanel = BuildHeaderControl(entry);
                    single_HeaderPanel.Children.Clear();
                    single_HeaderPanel.Children.Add(headerPanel);

                    // The content of the drug entry
                    //
                    var contentPanel = BuildEntryContentControl(entry);
                    single_ContentContainer.Content = contentPanel;

                    single_RootContainer.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    containerRoot.Visibility = System.Windows.Visibility.Visible;

                    foreach (var entry in drug.Entries)
                    {
                        var p = new PanoramaItem();
                        p.Content = new ScrollViewer { Content = BuildEntryContentControl(entry) };
                        p.Header = BuildHeaderControl(entry);
                        containerRoot.Items.Add(p);
                    }
                }

            }
        }

        /// <summary>
        /// Create controls to display the content of the drug entry
        /// </summary>
        private static StackPanel BuildEntryContentControl(DrugEntry entry)
        {
            StackPanel stackPanel = new StackPanel();
            stackPanel.Style = (Style)Application.Current.Resources["DrugItemContainerPanelStyle"];

            foreach (var x in entry.Items)
            {
                if (!string.IsNullOrEmpty(x.Value))
                {
                    StackPanel sp = new StackPanel();
                    sp.Children.Add(new TextBlock { Text = x.Name, Style = (Style)Application.Current.Resources["DrugItemTitleStyle"] });
                    sp.Children.Add(new TextBlock { Text = x.Value, Style = (Style)Application.Current.Resources["DrugItemTextStyle"] });
                    stackPanel.Children.Add(sp);
                }
            }
            return stackPanel;
        }

        /// <summary>
        /// Create controls to display the header of the drug entry
        /// </summary>
        private static StackPanel BuildHeaderControl(DrugEntry entry)
        {
            StackPanel headerPanel = new StackPanel();
            headerPanel.Children.Add(new TextBlock { Text = entry.Name, Style = (Style)Application.Current.Resources["DrugItemHeaderStyle"] });
            if (!string.IsNullOrEmpty(entry.ApprovalNumber))
            {
                headerPanel.Children.Add(new TextBlock { Text = entry.ApprovalNumber, Style = (Style)Application.Current.Resources["DrugItemSubHeaderStyle"] });
            }
            if (!string.IsNullOrEmpty(entry.Manufacturer))
            {
                headerPanel.Children.Add(new TextBlock { Text = entry.Manufacturer, Style = (Style)Application.Current.Resources["DrugItemSubHeaderStyle"] });
            }
            return headerPanel;
        }
    }
}